package demo.android.com.mvpdemo.myHttpUtils.okhttp;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import java.io.IOException;
import java.util.Map;

import demo.android.com.mvpdemo.myHttpUtils.http.EngineCallback;
import demo.android.com.mvpdemo.myHttpUtils.http.IHttpRequest;
import demo.android.com.mvpdemo.myHttpUtils.http.SPHttpCache;
import demo.android.com.mvpdemo.myHttpUtils.http.Utils;
import demo.android.com.mvpdemo.myHttpUtils.retrofit.RetrofitErrorException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by hcDarren on 2017/8/26.
 *
 */

public class OKHttpRequest implements IHttpRequest {
    private SPHttpCache mHttpCache;

    public OKHttpRequest(){
        mHttpCache = new SPHttpCache();
    }

    @Override
    public void get(Context context, String url, Map<String, Object> params, final EngineCallback callback, final boolean cache) {
        Log.e("wangyan","OKHttpRequest---->"+url);
        OkHttpClient mOkHttpClient = new OkHttpClient();
        final String jointUrl = Utils.jointParams(url, params);  //链接
        // 缓存问题
        final String cacheJson = mHttpCache.getCache(jointUrl);

        // 写一大堆处理逻辑 ，内存怎么扩展等等
        if (cache && !TextUtils.isEmpty(cacheJson)) {
           // callback.onSuccess(cacheJson);
        }

        Request.Builder requestBuilder = new Request.Builder().url(jointUrl).tag(context);

        //可以省略，默认是GET请求
        Request request = requestBuilder.build();
        mOkHttpClient.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                callback.onFailure(new RetrofitErrorException(e.getMessage()));
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                 String resultJson = null;
               // if(ConstantValue.RESPONSE_SUCCESS == response.code()){  //服务返回0 表示成功
                    resultJson = response.body().string();
                    if(resultJson == null){
                        callback.onFailure(new RetrofitErrorException("返回数据为空"));
                    }
                    if (cache && resultJson.equals(cacheJson)) {
                        return;
                    }
                    callback.onSuccess(resultJson);
                    if (cache) {
                        mHttpCache.saveCache(jointUrl, resultJson);
                    }
                //}

            }
        });

    }

    @Override
    public void post(Context context, String url, Map<String, Object> params, EngineCallback callback, boolean cache) {

    }

    @Override
    public void download(Context context, String url, Map<String, Object> params, EngineCallback callback) {

    }

    @Override
    public void upload(Context context, String url, Map<String, Object> params, EngineCallback callback) {

    }
}
